package com.qibo.baselib.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.noober.background.BackgroundLibrary;
import com.qibo.baselib.utils.AppManager;

import java.util.ArrayList;


/**
 * Created by zhuzidong
 * on 2017/10/10.
 * Email:591079255@qq.com
 *
 * 只进行activity的初始化和跳转会用到的基础功能，不涉及定制功能，
 * 定制功能由其子类实现；
 *
 * 可以在基类中添加 泄露检测等功能， 在onCreate中添加，在onDestroy中解除；
 * 基类继承 AppCompatActivity 是为了 对v4，v7等Fragment的支持，appCompatActivity
 * 自己本身继承了 FragmentActivity ；
 */

public abstract class BaseActivity extends AppCompatActivity
{
    private static final String CONSTANT_TYPE_STRING = "constant_string";//implements CustomAdapt
    private static final String CONSTANT_TYPE_INTEGER = "constant_integer";
    private static final String CONSTANT_TYPE_BOOLEAN = "constant_boolean";
    private static final String CONSTANT_TYPE_ARRAYLIST = "constant_arraylist";
    private static final String CONSTANT_TYPE_BUNDLE = "constant_bundle";
    /**上下文*/
    public Context mContext;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        BackgroundLibrary.inject(this);
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null)
        {
            getSupportActionBar().hide();
        }
        setContentView(getContentViewId());
        mContext = getApplicationContext();;
        AppManager.getInstance().addActivity(this);

        initBeforeStart();
        initView();
        initAction();

        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                initData();
            }
        },500);


    }

    protected void initBeforeStart()
    {

    }

    protected abstract int getContentViewId();

    protected abstract void initView();

    protected abstract void initAction();

    protected abstract void initData();



    @Override
    protected void onStop()
    {
        super.onStop();
    }

    @Override
    protected void onDestroy()
    {
        AppManager.getInstance().killActivity(this);
        super.onDestroy();
    }


    public void switchActivity(Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        startActivity(intent);
    }

    public void switchActivityForResult(int requestCode,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        startActivityForResult(intent,requestCode);
    }

    public void switchActivityWithBundle(Bundle bundles, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_BUNDLE,bundles);
        startActivity(intent);
    }

    public void switchActivityWithBundleForResult(int requestCode, Bundle bundles, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_BUNDLE,bundles);
        startActivityForResult(intent,requestCode);
    }


    public Bundle getTranslateBundle()
    {
        return getIntent().getBundleExtra(CONSTANT_TYPE_BUNDLE);
    }


    public void switchActivityWithInteger(int data,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_INTEGER,data);
        startActivity(intent);
    }

    public void switchActivityWithIntegerForResult(int data,int requestCode,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_INTEGER,data);
        startActivityForResult(intent,requestCode);
    }

    public int getTranslateInteger()
    {
        return getIntent().getIntExtra(CONSTANT_TYPE_INTEGER,-1);
    }

    public void switchActivityWithString(String data, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_STRING,data);
        startActivity(intent);
    }

    public void switchActivityWithStringForResult(String data, int requestCode, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_STRING,data);
        startActivityForResult(intent,requestCode);
    }

    public String getTranslateString()
    {
        return getIntent().getStringExtra(CONSTANT_TYPE_STRING);
    }

    public void switchActivityWithBoolean(boolean data,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_BOOLEAN,data);
        startActivity(intent);
    }

    public void switchActivityWithBooleanForResult(boolean data,int requestCode,Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_BOOLEAN,data);
        startActivityForResult(intent,requestCode);
    }

    public boolean getTranslateBoolean()
    {
        return getIntent().getBooleanExtra(CONSTANT_TYPE_BOOLEAN,false);
    }

    public void switchActivityWithArrayList(ArrayList data, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_ARRAYLIST,data);
        startActivity(intent);
    }

    public void switchActivityWithArrayListForResult(ArrayList data, int requestCode, Class<? extends Activity> typeClass)
    {
        Intent intent = new Intent(this,typeClass);
        intent.putExtra(CONSTANT_TYPE_ARRAYLIST,data);
        startActivityForResult(intent,requestCode);
    }

    public ArrayList<Integer> getTranslateIntegerArrayList()
    {
        return getIntent().getIntegerArrayListExtra(CONSTANT_TYPE_ARRAYLIST);
    }

    public ArrayList<String> getTranslateStringArrayList()
    {
        return getIntent().getStringArrayListExtra(CONSTANT_TYPE_ARRAYLIST);
    }
//    @Override
//    public boolean isBaseOnWidth() {
//        return false;
//    }
//
//    @Override
//    public float getSizeInDp() {
//        return 667;
//    }
}
